<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/8/1
 * Time: 11:03
 */
namespace app\admin\controller;
use think\Db;
class Failed extends Base {

    public function index(){
        if(request()->isAjax()){
            extract(input());
            $map =array();
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            if(isset($keyword)&&$keyword!="")
            {
                $map['a.media_title'] = ['like',"%" . $keyword . "%"];
            }
            $map['is_publish'] = 1;
            $count = Db::table('up_go_school')->alias('a')
                ->join('up_u_user ua','ua.user_id = a.media_user_id','left')
                ->where($map)
                ->count();
            $list = Db::table('up_go_school')->alias('a')
                ->join('up_u_user ua','ua.user_id = a.media_user_id','left')
                ->where($map)
                ->field('a.*,ua.user_username')
                ->order('a.media_id desc')
                ->page($page, $limit)
                ->select();//计算总页面
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }
    //删除
    public function del(){
        $id  = input('param.id');
        $where['type'] = 9;
        $where['f_id'] = $id;
        $info = Db::table('up_comment')->where($where)->count();
        $info1 = Db::table('up_comment')->where($where)->select();
        if($info>0){
            foreach ($info1 as $key=>&$vo){
                $where1['f_id'] = $vo['comment_id'];
                $count1 = Db::table('up_comment_reply')->where($where1)->count();
                if($count1>0){
                    Db::table('up_comment_reply')->where($where1)->delete();
                }
            }
            Db::table('up_comment')->where($where)->delete();
        }
        $res =  Db::table('up_go_school')->where('media_id',$id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }



    public function detail()
    {
        $id = input('param.id');
        $list = Db::table('up_go_school')->alias('a')
            ->join('up_u_user ua','ua.user_id = a.media_user_id','left')
            ->where('a.media_id',$id)
            ->field('a.*,ua.user_username')
            ->find();
        $this->assign('list',$list);
        return $this->fetch();
    }
}